package com.ss.android.newmedia;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.webkit.WebView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.b.UserAgentCompat;
import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.exception.HttpResponseException;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.EventSender;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.LegacyHeader;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONArray;
import org.json.JSONObject;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class MediaAppUtil {
    static final class a extends AsyncTask {
        public int a;
        private List b;
        private boolean c;

        public a(List urls, boolean arg5) {
            this.b = null;
            this.a = 0;
            if (urls != null && !urls.isEmpty()) {
                this.b = new ArrayList();
                this.b.addAll(urls);
                if (Logger.debug()) {
                    Logger.d("MediaAppUtil", "urls = " + urls.toString());
                }
            } else if (Logger.debug()) {
                Logger.d("MediaAppUtil", "urls is empty");
            }
            this.c = arg5;
        }

        private Integer a() {
            JSONObject v10;
            Object v2;
            if (this.b != null && !this.b.isEmpty()) {
                Iterator v15 = this.b.iterator();
                do {
                    label_13:
                    if (v15.hasNext()) {
                        v2 = v15.next();
                        if (!TTUtils.isValidUrl(((String) v2))) {
                            continue;
                        }

                        String v2_1 = this.a(((String) v2));
                        String v12 = this.c ? android.support.design.a.g(v2_1) : v2_1;
                        if (this.a == 0) {
                            try {
                                if (!EventSender.inst().mSenderEnable) {
//                            goto label_37;
                                } else {
                                    JSONObject v2_3 = new JSONObject();
                                    v2_3.put("TrackUrl", v12);
                                    EventSender.inst().putEvent(v2_3);
                                }
                            } catch (Exception v2_2) {
                            }
                        }

//                label_37:
                        int v13 = 0;
                        while (true) {
                            label_38:
                            if (v13 > 0) {
                                break;
                            }

                            int v2_5 = 40960;
                            try {
                                NetworkUtils.doGet(v2_5, v12, false);
                                v10 = new JSONObject();
                                v10.put("url", v12);
                                MobClickCombiner.onEvent(MediaAppUtil.sContext, "ad_stat", "track_url", 200, 0, v10);
                                break;
                            } catch (Exception v11) {
                                Logger.d("AdsStats", "error=" + v11.toString());
                                if ((v11 instanceof HttpResponseException)) {
                                    try {
                                        v2_5 = ((HttpResponseException) v11).getStatusCode();
                                        v10 = new JSONObject();
                                        v10.put("url", v12);
                                        MobClickCombiner.onEvent(MediaAppUtil.sContext, "ad_stat", "track_url", Long.valueOf(((long) v2_5)).longValue(), 0, v10);
                                    } catch (Exception v2_2) {
                                    }
                                }

                                if (!(v11 instanceof ConnectTimeoutException) && !(v11 instanceof SocketTimeoutException)) {
                                    break;
                                }

                                ++v13;
                                continue;
                            }
                        }
                    } else {
                        Integer v2_4 = Integer.valueOf(0);
                        return v2_4;
                    }
                }
                while (true);


            }

            return Integer.valueOf(0);
        }

        private String a(String arg5) {
            if (!android.support.design.a.isTextEmpty(arg5)) {
                try {
                    switch (this.a) {
                        case 0: {
                            if ((arg5.contains("{TS}")) || (arg5.contains("__TS__"))) {
                                long v0_1 = System.currentTimeMillis();
                                arg5 = arg5.replace("{TS}", String.valueOf(v0_1)).replace("__TS__", String.valueOf(v0_1));
                            }

                            String v0_2 = AppLog.deviceId();
                            if (!arg5.contains("{UID}") && !arg5.contains("__UID__")) {
                                return arg5;
                            }

                            if (android.support.design.a.isTextEmpty(v0_2)) {
                                return arg5;
                            }

                            arg5 = arg5.replace("{UID}", ((CharSequence) v0_2)).replace("__UID__", ((CharSequence) v0_2));
                        }
                    }
                    return arg5;

                } catch (Exception v0) {
                }
            }

            return arg5;
        }

        protected final Object doInBackground(Object[] arg2) {
            return this.a();
        }
    }

    public static Context sContext = null;
    private static String mUserAgent = null;
    private static boolean newWebViewTried = false;

    public static boolean a(String requestUrl, String arg6) {
        boolean v0 = false;
        if (requestUrl != null && arg6 != null) {
            if (requestUrl.equals(arg6)) {
                v0 = true;
            } else {
                Uri v1 = Uri.parse(requestUrl);
                Uri v2 = Uri.parse(arg6);
                if ((TextUtils.isEmpty(v1.getPath())) && (TextUtils.isEmpty(v1.getQuery()))) {
                    requestUrl = requestUrl + "/";
                }

                if ((TextUtils.isEmpty(v2.getPath())) && (TextUtils.isEmpty(v2.getQuery()))) {
                    arg6 = arg6 + "/";
                }

                int v1_1 = requestUrl.indexOf('#');
                if (v1_1 > 0) {
                    requestUrl = requestUrl.substring(0, v1_1);
                }

                v1_1 = arg6.indexOf('#');
                if (v1_1 > 0) {
                    arg6 = arg6.substring(0, v1_1);
                }

                v0 = requestUrl.equals(arg6);
            }
        }

        return v0;
    }

    public static void a() {
        List v0 = MediaAppData.provide().aP();
        if (v0 != null && !v0.isEmpty()) {
            a v1 = new MediaAppUtil.a(v0, true);
            v1.a = 0;
            v1.execute();
        }
    }

    public static Uri a(Context context, String arg8) {
        Cursor v1 = null;
        Uri v0;
        Uri v6 = null;
        if (android.support.design.a.isTextEmpty(arg8)) {
            v0 = v6;
            return v0;
        }

        try {
            v1 = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{"_id"}, "_data= ?", new String[]{arg8}, null);
            if (!v1.moveToFirst()) {
                return null;
            }
            v0 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Long.parseLong(v1.getString(0)));
            return v0;
        } catch (Exception v0_2) {
            try {
                v0_2.printStackTrace();
                if (v1 != null) {
                    v1.close();
                    return v6;
                }
            } catch (Throwable v0_1) {
            }


            return v6;
        }
    }

    public static String getUserAgent(Context context, WebView webView) {
        String userAgentString;
        if (webView != null) {
            userAgentString = webView.getSettings().getUserAgentString();
            if (!android.support.design.a.isTextEmpty(userAgentString)) {
                mUserAgent = userAgentString;
                return userAgentString;
            }
        }

        if (!android.support.design.a.isTextEmpty(mUserAgent)) {
            return mUserAgent;
        }

        userAgentString = UserAgentCompat.getUserAgent(context);
        mUserAgent = userAgentString;
        if (!android.support.design.a.isTextEmpty(userAgentString)) {
            return mUserAgent;
        }

        if (!newWebViewTried && webView == null && context != null && context instanceof Activity) {
            newWebViewTried = true;
            try {
                WebView temWebView = new WebView(context);
                mUserAgent = temWebView.getSettings().getUserAgentString();
                temWebView.destroy();
            } catch (Throwable throwable) {

            }
        }

        return mUserAgent;
    }

    public static void setContext(Context context) {
        sContext = context;
    }

    public static void reportAppDownloadBanned(Context arg9, JSONObject arg10) {
        if (arg9 != null) {
            try {
                String v3 = arg10.optString("label");
                JSONObject v8 = arg10.optJSONObject("ext_json");
                if (android.support.design.a.isTextEmpty(v3)) {
                    return;
                }
                MobClickCombiner.onEvent(arg9, "wap_stat", "app_download_banned", v3, 0, 0, v8);
            } catch (Exception exception) {
            }
        }
    }

    public static void a(String arg2, Context arg3) {
        if (!android.support.design.a.isTextEmpty(arg2)) {
            ArrayList v0 = new ArrayList();
            ((List) v0).add(arg2);
            a(((List) v0), arg3, false, 0);
        }
    }

    public static void a(List<String> arg5, Context context, boolean arg7, int arg8) {
        if (context != null && arg5 != null && !arg5.isEmpty()) {
            if (!NetworkUtils.isNetworkAvailable(context)) {
                MediaAppData mediaAppData = MediaAppData.provide();
                if (arg5 == null) {
                    return;
                }

                if (arg5.isEmpty()) {
                    return;
                }

                try {
                    JSONArray jsonArray = android.support.design.a.isTextEmpty(mediaAppData.mAdTrackUrlList) ? new JSONArray() : new JSONArray(mediaAppData.mAdTrackUrlList);
                    Iterator<String> iterator = arg5.iterator();
                    while (iterator.hasNext()) {
                        Object v0_1 = iterator.next();
                        if (android.support.design.a.isTextEmpty(((String) v0_1))) {
                            continue;
                        }

                        if (!TTUtils.isValidUrl(((String) v0_1))) {
                            continue;
                        }

                        jsonArray.put(v0_1);
                    }

                    mediaAppData.mAdTrackUrlList = jsonArray.toString();
                } catch (Exception v0) {
                    mediaAppData.mAdTrackUrlList = null;
                }
            } else {
                a v0_2 = new a(arg5, arg7);
                v0_2.a = arg8;
                v0_2.execute(new Object[0]);
            }
        }
    }

    public static void a(StringBuilder arg0) {
        AppLog.a(arg0);
    }

    public static void appendAdditionHttpHeaders(HashMap<String,String> additionalHttpHeaders, String userAgent, JSONObject jsonObject) {
        if (jsonObject != null) {
            Iterator<String> iterator = jsonObject.keys();
            while (iterator != null) {
                if (!iterator.hasNext()) {
                    break;
                }

                try {
                    String key = iterator.next();
                    String value = jsonObject.optString(key);
                    if (android.support.design.a.isTextEmpty(((String) key))) {
                        continue;
                    }

                    if (android.support.design.a.isTextEmpty(value)) {
                        continue;
                    }

                    additionalHttpHeaders.put(key, value);
                } catch (Exception exception) {
                }
            }
        }

        if (!android.support.design.a.isTextEmpty(userAgent)) {
            additionalHttpHeaders.put("User-Agent", userAgent);
        }
    }

    public static void a(List<String> trackUrlList, Context context) {
        a(trackUrlList, context, false, 0);
    }

    public static void appendHeaders(List arg4, String userAgent, JSONObject arg6) {
        if (arg6 != null) {
            Iterator v1 = arg6.keys();
            while (v1 != null) {
                if (!v1.hasNext()) {
                    break;
                }

                try {
                    Object v0_1 = v1.next();
                    String v2 = arg6.optString(((String) v0_1));
                    if (android.support.design.a.isTextEmpty(((String) v0_1))) {
                        continue;
                    }

                    if (android.support.design.a.isTextEmpty(v2)) {
                        continue;
                    }

                    arg4.add(new LegacyHeader(((String) v0_1), v2));
                } catch (Exception v0) {
                }
            }
        }

        if (!android.support.design.a.isTextEmpty(userAgent)) {
            arg4.add(new LegacyHeader("User-Agent", userAgent));
        }
    }

    public static boolean a(int arg1) {
        return arg1 == 13 || arg1 == 14 || arg1 == 15;
    }

    public static boolean a(Context arg3, String arg4, String arg5) {
        boolean v0 = false;
        int v1 = getVersionCode(arg3, arg5);
        if (v1 != -1 && !TextUtils.isEmpty(((CharSequence) arg4))) {
            try {
                if (Integer.valueOf(arg4).intValue() <= v1) {
                    return v0;
                }
            } catch (NumberFormatException v1_1) {
                return v0;
            }

            v0 = true;
        }

        return v0;
    }

    private static int getVersionCode(Context context, String packageName) {
        int versionCode = -1;
        if (context != null && !TextUtils.isEmpty(packageName)) {
            try {
                versionCode = context.getPackageManager().getPackageInfo(packageName, 0).versionCode;
            } catch (PackageManager.NameNotFoundException nameNotFoundException) {
            }
        }

        return versionCode;
    }

    public static void b(List arg2, Context arg3) {
        a(arg2, arg3, true, 0);
    }
}

